﻿using System;
using System.Collections.Generic;
using RentalSystem.Data.EF.Core;

namespace RentalSystem.Data.EF.Dao
{
    public class EFDiscsDao : IDiscsDao
    {
        #region Queries

        private static string GetQuery;
        private static string ListMovieDiscFromAgencyQuery;

        #endregion

        private EFManager<Discs> manager;

        public EFDiscsDao(RentalDatabaseEntities repository)
        {
            GetQuery = "SELECT VALUE d FROM Discs AS d WHERE d.DiscGuid = @Guid;";
            ListMovieDiscFromAgencyQuery = "SELECT VALUE d FROM Discs AS d WHERE d.MovieGuid = @MovieGuid;";

            this.manager = new EFManager<Discs>(repository);
        }

        #region IDao1Key

        public Discs Add(Discs entity)
        {
            manager.Database.AddToDiscs(entity);
            manager.Save();
            return entity;
        }

        public Discs Get(string id)
        {
            QueryParam[] parameters = new QueryParam[]
                {
                    new QueryParam("@Guid", id)
                };

            var discs = manager.SQLQueryToEntity(GetQuery, parameters);

            foreach (Discs disc in discs)
            {
                return disc;
            }

            return null;
        }

        public void Delete(string id)
        {
            manager.Database.Discs.DeleteObject(Get(id));
            manager.Save();
        }

        public void Delete(Discs entity)
        {
            Delete(entity.DiscGuid);
        }

        public void Update(Discs entity, uint fields)
        {
            Discs fromDb = Get(entity.DiscGuid);

            if (fromDb != null)
            {
                if ((fields & (uint)DiscFields.Agency) == (uint)DiscFields.Agency)
                    fromDb.Agencies = entity.Agencies;
                if ((fields & (uint)DiscFields.Count) == (uint)DiscFields.Count)
                    fromDb.DiscCount = entity.DiscCount;
                if ((fields & (uint)DiscFields.DiscType) == (uint)DiscFields.DiscType)
                    fromDb.DiscTypes = entity.DiscTypes;
                if ((fields & (uint)DiscFields.IsAvailable) == (uint)DiscFields.IsAvailable)
                    fromDb.DiscIsAvailable = entity.DiscIsAvailable;
                if ((fields & (uint)DiscFields.IsHired) == (uint)DiscFields.IsHired)
                    fromDb.DiscIsHired = entity.DiscIsHired;
                if ((fields & (uint)DiscFields.Movie) == (uint)DiscFields.Movie)
                    fromDb.Movies = entity.Movies;

                manager.Save();
            }
        }

        public List<Discs> List()
        {
            throw new NotImplementedException();
        }

        #endregion

        public List<Discs> ListMovieDiscFromAgency(string movieGuid, int agencyId)
        {
            QueryParam[] parameters = new QueryParam[]
                {
                    new QueryParam("@MovieGuid", movieGuid)
                };

            List<Discs> discs = new List<Discs>();

            var result = manager.SQLQueryToEntity(ListMovieDiscFromAgencyQuery,parameters);

            foreach (Discs disc in result)
            {
                if(disc.AgencyGuid == agencyId)
                    discs.Add(disc);
            }

            return discs;
        }
    }
}
